//
//  InputToolbar.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 1/11/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "InputToolbar.h"
#import "BorderButton.h"

@implementation InputToolbar

#pragma mark - Lifecycle
- (id)init
{
    self = [super init];
    if (self)
    {
        [self initialzeCloseButton];
        self.backgroundColor = COLOR_GRAY;
        
        self.frame = self.frame;
    }
    
    return self;
}

- (void)addRightButtonWithTitle:(NSString *)rightTitle
{
    if (_rightButton)
    {
        return;
    }
    _rightButton = [[BorderButton alloc] initWithTitle:rightTitle];
    [_rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_rightButton];
}

- (void)removeRightButton
{
    if (_rightButton == nil)
    {
        return;
    }
    
    [_rightButton removeFromSuperview];
    _rightButton = nil;
}

- (void)initialzeCloseButton
{
    _closeButton = [[BorderButton alloc] initWithTitle:NSLocalizedString(@"ĐÓNG", nil)];
    [_closeButton addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_closeButton];
}

#pragma mark - Logic functions
- (void)setFrame:(CGRect)frame
{
    frame.size = CGSizeMake(SCREEN_SIZE.width, HEI_HEADER_VIEW);
    [super setFrame:frame];
    
    _closeButton.frame = RECT(PADDING_SMALL, PADDING_SMALL, [_closeButton.titleLabel widthString] + PADDING_SMALL*2, self.height - PADDING_SMALL*2);
    
    int widRightButton = [_rightButton.titleLabel widthString] + PADDING_SMALL*2;
    _rightButton.frame = RECT(self.width - widRightButton - PADDING_SMALL, _closeButton.top, widRightButton, _closeButton.height);
}

#pragma mark - Delegate
- (void)closeButtonPressed:(BorderButton*)sender
{
    if ([_toolbarDelegate respondsToSelector:@selector(inputToolbarDidPressCloseButton:)])
    {
        [_toolbarDelegate inputToolbarDidPressCloseButton:self];
    }
}

- (void)rightButtonPressed:(BorderButton*)sender
{
    if ([_toolbarDelegate respondsToSelector:@selector(inputToolbarDidPressRightButton:)])
    {
        [_toolbarDelegate inputToolbarDidPressRightButton:self];
    }
}

@end
